/**
 * 
 */
package com.taobao.snowsnow.hibernate.service.impl;

import java.util.List;

import com.taobao.snowsnow.hibernate.dao.BaseDao;
import com.taobao.snowsnow.hibernate.service.BaseService;

/**
 * @author Tux
 *
 * 创建时间: 2011-6-7 上午11:13:00
 */
public class BaseServiceImpl implements BaseService {
	private BaseDao bdao;
//	private Lazy lazy = new Lazy();
	
	public int create(Object entity) throws Exception {
		try{
			bdao.create(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("添加出错!");
			throw new RuntimeException("运行期异常 unchecked异常");
		}
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.service.BaseService#remove(java.lang.Object)
	 */
	public int remove(Object entity) throws Exception {
		try {
			bdao.remove(entity);
			return 1;
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("删除出错!");
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.service.BaseService#removeList(java.util.List)
	 */
	@SuppressWarnings("rawtypes")
	public int removeList(List list) throws Exception {
		try{
			bdao.removeList(list);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("批量删除出错");
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.service.BaseService#update(java.lang.Object)
	 */
	public int update(Object entity) throws Exception {
		try {
			bdao.update(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("更新出错!");
			throw e;
		}
	}

	public void setBdao(BaseDao bdao) {
		this.bdao = bdao;
	}

	public BaseDao getBdao() {
		return bdao;
	}

	@Override
	public int saveOrUpdate(Object entity) throws Exception {
		try {
			bdao.saveOrUpdate(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("新建或者更新出错!");
			throw e;
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public long countByCriteria(Class c) {
		try {
			return bdao.countByCriteria(c);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error at countByCriteria!");
			return 0l;
		}
	}
}
